﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LDSP.BRL;
using System.Data;

namespace LDSP.UI.Admin
{
    public partial class AddEnterpriseUser : BRL.BaseSysAdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (this.RB())
                {
                    if (!IsPostBack)
                    {
                        this.CheckQS();
                        this.InitialControls();
                        this.InitDataSelect();
                    }
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        private void InitialControls()
        {
            this.tr_supportManageFavorite.Visible = IsAdd || EnterpriseUser.Enterprise.SupportManageFavorite;
            this.tr_supportSMS.Visible = IsAdd || EnterpriseUser.Enterprise.SupportSMS;
        }

        protected EnterpriseUser m_enterpriseUser = null;

        protected int LastID
        {
            get { return this.GetVSInt("LastID"); }
            set
            {
                this.SetVS("LastID", value);
                m_enterpriseUser = null;
            }
        }
        protected EnterpriseUser EnterpriseUser
        {
            get
            {
                if (m_enterpriseUser == null)
                {
                    m_enterpriseUser = new EnterpriseUser();
                    if (LastID != 0)
                    {
                        m_enterpriseUser.GetProperties(LastID);
                    }
                }
                return m_enterpriseUser;
            }
        }
        protected bool IsAdd
        {
            get { return LastID == 0; }
        }

        protected void CheckQS()
        {
            LastID = this.GetInt(this.Request.QueryString["id"]);
            if (EnterpriseUser.ID == 0)
            {
                LastID = 0;
            }
        }
        protected bool RB()
        {
            return this.AllowSystemUser();
        }
        protected void SetStatus()
        {
            this.lbl_op.Text = IsAdd ? "【添加】" : "【修改】";
            this.lbl_op.Style["color"] = IsAdd ? "red" : "blue";
            this.ClientComfirm(this.btn_ok, string.Format("您确定执行{0}操作吗？", IsAdd ? "添加" : "修改"));
        }
        protected void InitDataSelect()
        {
            this.txt_code.Text = EnterpriseUser.Code;
            this.txt_name.Text = EnterpriseUser.Name;
            this.txt_comment.Text = EnterpriseUser.Comment;
            this.txt_pwd.Text = EnterpriseUser.Password;
            this.chb_isValid.Checked = EnterpriseUser.IsValid;
            this.txt_email.Text = EnterpriseUser.Email;
            this.chb_supportManageFavorite.Checked = EnterpriseUser.SupportManageFavorite;
            this.chb_supportSMS.Checked = EnterpriseUser.SupportSMS;

            this.BindDDLUserType();
            this.BindDDLEnterprise();
            this.SetStatus();
        }

        protected void BindDDLEnterprise()
        {
            DataTable dt = new Enterprise().GetAll();
            this.ddl_enterprise.Items.Clear();
            this.ddl_enterprise.Items.Add(new ListItem("--请选择企业--", "0"));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ddl_enterprise.Items.Add(new ListItem(dt.Rows[i]["code"].ToString(), dt.Rows[i]["id"].ToString()));
            }
            try
            {
                this.ddl_enterprise.SelectedIndex = 0;
                this.ddl_enterprise.SelectedValue = EnterpriseUser.EnterpriseID.ToString();
            }
            catch { }
        }
        protected void BindDDLUserType()
        {
            this.ddl_userType.Items.Clear();
            this.ddl_userType.Items.Add(new ListItem(EnterpriseUserType.企业管理员.ToString(), ((int)EnterpriseUserType.企业管理员).ToString()));
            this.ddl_userType.Items.Add(new ListItem(EnterpriseUserType.普通用户.ToString(), ((int)EnterpriseUserType.普通用户).ToString()));
            try
            {
                this.ddl_userType.SelectedIndex = 0;
                this.ddl_userType.SelectedValue = ((int)EnterpriseUser.UserType).ToString();
            }
            catch { }
        }
        protected void ValidateDataBefore()
        {
            if (this.ddl_enterprise.SelectedIndex == 0)
            {
                throw new BYFW.BOL.CustomException("请选择企业！");
            }
        }
        protected void ValidateDataAfter()
        {
            if (!this.IsName(EnterpriseUser.Code))
            {
                throw new BYFW.BOL.CustomException("名称只能包含字母，数字，-和下划线！");
            }
            if (!EnterpriseUser.CanUpdate(null))
            {
                throw new BYFW.BOL.CustomException("该名称用户已存在，请更换！");
            }
        }
        protected void SetData()
        {
            this.ValidateDataBefore();
            EnterpriseUser.Code = this.txt_code.Text.Trim();
            EnterpriseUser.Name = this.txt_name.Text.Trim();
            EnterpriseUser.Comment = this.txt_comment.Text;
            EnterpriseUser.Password = this.txt_pwd.Text;
            EnterpriseUser.UserType = (EnterpriseUserType)this.GetInt(this.ddl_userType.SelectedValue);
            EnterpriseUser.EnterpriseID = this.GetInt(this.ddl_enterprise.SelectedValue);
            EnterpriseUser.IsValid = this.chb_isValid.Checked;
            EnterpriseUser.SupportManageFavorite = this.chb_supportManageFavorite.Checked;
            EnterpriseUser.SupportSMS = this.chb_supportSMS.Checked;
            EnterpriseUser.Email = this.txt_email.Text.Trim();
            this.ValidateDataAfter();
        }
        protected void Add()
        {
            this.SetData();
            EnterpriseUser.Add();
            this.LastID = 0;
            this.JSAlert("添加成功！");
        }
        protected void Update()
        {
            string tmp = EnterpriseUser.Name;
            this.SetData();
            if (tmp != EnterpriseUser.Name)
            {
                new DownloadLog().UpdateByEnterpriseUser(EnterpriseUser.ID, EnterpriseUser.Name);
            }
            EnterpriseUser.Update();
            this.JSAlert("更新成功！");
        }

        protected void btn_add_Click(object sender, EventArgs e)
        {
            try
            {
                this.LastID = 0;
                this.InitDataSelect();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void btn_ok_Click(object sender, EventArgs e)
        {
            try
            {
                if (IsAdd)
                {
                    this.Add();
                }
                else
                {
                    this.Update();
                }
                this.InitDataSelect();

            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
    }
}